/**
 * created by 小卷毛, 2022-03-01
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.*
import com.rzico.sbl.ui.report.other.ReportUrl
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：报货自提操作相关
 * 作 者：小卷毛  2022-03-01 17:24
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class ReportInviteViewModel : DialogViewModel() {

    /**
     * 库管入库空桶单列表
     */
    fun inviteList(
        page: Int,
        status: String,
        onSuccess: (ArrayList<BarrelData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<BarrelData>>>(ReportUrl.getBaseUrl().inviteList)
            .params(
                getParams(
                    "status" to status,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<BarrelData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 水站列表
     */
    fun stationList(
        page: Int,
        keyWord: String,
        onSuccess: (ArrayList<MasterData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<MasterData>>>(ReportUrl.getBaseUrl().inviteShop)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ArrayList<MasterData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 桶品牌列表
     */
    fun barrelList(shopId: String) =
        OkGo.get<BaseResponse<ArrayList<DispatchBucket>>>(ReportUrl.getBaseUrl().inviteBrl)
            .params(
                getParams("id" to shopId)
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<DispatchBucket>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

    /**
     * 企业商品分类
     */
    fun kindList(shopId: String) =
        OkGo.get<BaseResponse<ArrayList<CommonData>>>(ReportUrl.getBaseUrl().productMCat)
            .params(getParams("id" to shopId))
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

    /**
     * 企业商品列表
     */
    fun goodsList(
        page: Int,
        shopId: String,
        kindId: String,
        keyWord: String,
        onSuccess: (ArrayList<ReportGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<ReportGoodsData>>>(ReportUrl.getBaseUrl().reportGoods)
            .params(
                getParams(
                    "id" to shopId,
                    "productCategoryId" to kindId,
                    "keyword" to keyWord,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<ReportGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 保存/修改入库
     */
    fun inviteSave(url: String, vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = url,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 入库单详情
     */
    fun inviteDetail(inviteId: String) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().inviteDet,
            params = getParams("id" to inviteId)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}